package com.cee.admin.modules.cfd.controller;

import cn.hutool.core.io.resource.ResourceUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cee.admin.common.BaseController;
import com.cee.common.core.service.CommonService;
import com.cee.common.exception.GlobalException;
import com.cee.common.spring.SpringContextHolder;
import com.cee.common.utils.exportWord.WordTmpToNewWordUtils;
import com.cee.common.web.result.Result;
import com.cee.common.web.result.ResultCode;
import com.cee.domain.modules.cfd.entity.CfdCarbonFootprintCertificateReport;
import com.cee.domain.modules.cfd.vo.CfdCarbonFootprintCertificateReportVo;
import com.cee.domain.modules.sys.entity.SysFrontUser;
import com.cee.ds.common.utils.BasePrincipalUtils;
import com.cee.ds.modules.cfd.service.ICfdCarbonFootprintCertificateReportService;
import com.cee.ds.modules.sys.service.ISysFrontUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Objects;

/**
 * <p>
 * 碳足迹认证表 前端控制器
 * </p>
 *
 * @author autoGenerator
 * @since 2024-04-08
 */
@Tag(name = "产品管理-碳足迹数据管理-碳足迹认证和报告")
@RestController
@RequestMapping("/api/cfd/carbon/footprint/certificate/report")
public class CfdCarbonFootprintCertificateReportController extends BaseController {

    @Autowired
    private ICfdCarbonFootprintCertificateReportService iCfdCarbonFootprintCertificateReportService;

    @Autowired
    private CommonService commonService;

    @Operation(summary = "分页查询碳足迹认证报告")
    @GetMapping("/page")
    public Result<Page<CfdCarbonFootprintCertificateReportVo>> page(@RequestParam Long current, @RequestParam Long size, CfdCarbonFootprintCertificateReportVo footprintDataVo) {
        footprintDataVo.setCityId(BasePrincipalUtils.currentUser().getCityId());
        return success(iCfdCarbonFootprintCertificateReportService.queryPageVo(new Page<>(current,size),footprintDataVo));
    }

    @Operation(summary = "上传报告")
    @PostMapping("/upload")
    @RequiresPermissions("cfd:report:upload")
    public Result<Boolean> upload(@RequestParam Long id, @RequestParam String reportFile) {
        checkPermWithCity(id);
        CfdCarbonFootprintCertificateReport report = new CfdCarbonFootprintCertificateReport();
        report.setId(id);
        report.setReportFile(reportFile);
        return success(commonService.updateWithFile(iCfdCarbonFootprintCertificateReportService,report, CfdCarbonFootprintCertificateReport::new));
    }

    @Operation(summary = "查看碳足迹认证证书")
    @GetMapping("/certificate/get/{id}")
    @RequiresPermissions("cfd:certificate:get")
    public void getCertificate(@PathVariable("id") Long id) throws IOException {
        checkPermWithCity(id);
        WordTmpToNewWordUtils.changWord(ResourceUtil.getStream("wordTemplate/ProductCarbonFootprintCertificate.docx"),wrapDownloadResponse("产品碳足迹认证"+System.currentTimeMillis()+".docx"),iCfdCarbonFootprintCertificateReportService.getReportInfo(id));
    }

    private void checkPermWithCity(Long id) {
        Integer cityId = BasePrincipalUtils.currentUser().getCityId();
        if (cityId != null) {
            Long belongUser = iCfdCarbonFootprintCertificateReportService.getByIdOrEx(id).getBelongUser();
            SysFrontUser frontUser = SpringContextHolder.getBean(ISysFrontUserService.class).getByIdOrEx(belongUser);
            if (!Objects.equals(cityId, frontUser.getCityId())) {
                throw new GlobalException(ResultCode.NOT_PERMISSION);
            }
        }
    }
}
